package ua.kiev.tosik.price.modules.clients.models;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import org.apache.log4j.Logger;
import ua.kiev.tosik.price.models.Clients;
import ua.kiev.tosik.price.modules.clients.conf.ModuleConfig;
import ua.kiev.tosik.price.modules.clients.gui.EditorDialog;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class EditorForm {

	private static Logger logger = Logger.getLogger("ua.kiev.tosik.price.modules.clients.models");
	private ModuleConfig config;
	private JPanel panel;
	private String dialogHeaderTitle;
	private ImageIcon dialogHeaderImage;
	private ActionListener saveButtonAction;

	private String nameLabel, phoneLabel, emailLabel, addressLabel, homepageLabel, createDateLabel, lastUpdateLabel;

	private JTextField nameField, phoneField, emailField, homepageField;
	private JTextArea addressField;

	private JButton saveButton;
	private JButton closeButton;
	private JButton pricesButton;
	private JButton deleteButton;

	public EditorForm() {
		logger.debug("Initialize EditForm view");
		config = ModuleConfig.getInstance();
		initComponents();
		updateLocale();
		panel = createForm();
	}

	public JPanel getPanel() {
		return panel;
	}

	public void showEditButtons(boolean hide) {
		deleteButton.setVisible(hide);
		pricesButton.setVisible(hide);
	}

	private JPanel createForm() {
		FormLayout layout = new FormLayout(
			"right:pref, 10px, left:pref, 10px",
			"pref, 10px, pref, 10px, pref, 10px, pref, 10px, pref, 10px, pref, 10px, pref, 10px, pref, 10px,"+
			"pref, 10px, center:pref"
		);

		PanelBuilder builder = new PanelBuilder(layout);
		builder.setDefaultDialogBorder();
		CellConstraints cc = new CellConstraints();

		JLabel label = new JLabel(dialogHeaderImage);
		// int fontSize = label.getFont().getSize();
		label.setFont(new Font(label.getFont().getFontName(), Font.BOLD, 24));
		label.setText(dialogHeaderTitle);
		label.setHorizontalAlignment(SwingConstants.LEFT);
		builder.add(label, cc.xyw(1, 1, 3));
		builder.addSeparator("", cc.xyw(1, 3, 4));

		builder.addLabel(nameLabel, cc.xy(1, 5));
		builder.add(nameField, cc.xy(3, 5));

		builder.addLabel(phoneLabel, cc.xy(1, 7));
		builder.add(phoneField, cc.xy(3, 7));

		builder.addLabel(emailLabel, cc.xy(1, 9));
		builder.add(emailField, cc.xy(3, 9));

		builder.addLabel(homepageLabel, cc.xy(1, 11));
		builder.add(homepageField, cc.xy(3, 11));

		JScrollPane scrollBar = new JScrollPane(addressField, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		builder.addSeparator(addressLabel, cc.xyw(1, 13, 4));
		builder.add(scrollBar, cc.xyw(1, 15, 4));

		builder.add(new JSeparator(), cc.xyw(1, 17, 4));

		builder.add(createButtonBar(), cc.xyw(1, 19, 4));

		return builder.getPanel();
	}

	private void initComponents() {
		nameField = new JTextField(40);
		phoneField = new JTextField(30);
		emailField = new JTextField(30);
		homepageField = new JTextField(30);
		addressField = new JTextArea();
		addressField.setPreferredSize( new Dimension(200, 100) );

		saveButton = new JButton();
		closeButton = new JButton();
		deleteButton = new JButton();
		pricesButton = new JButton();
		closeButton.addActionListener( new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				EditorDialog.getInstance().close();
			}
		});
	}

	public void setSaveButtonAction(ActionListener saveButtonAction) {
		for ( ActionListener al: saveButton.getActionListeners() ) {
			saveButton.removeActionListener(al);
		}
		saveButton.addActionListener(saveButtonAction);
	}

	public void setPricesButtonAction(ActionListener pricesButtonAction) {
		for ( ActionListener al: pricesButton.getActionListeners() ) {
			pricesButton.removeActionListener(al);
		}
		pricesButton.addActionListener(pricesButtonAction);
	}

	public void setDeleteButtonAction(ActionListener deleteRecordAction) {
		for ( ActionListener al: deleteButton.getActionListeners() ) {
			deleteButton.removeActionListener(al);
		}
		deleteButton.addActionListener(deleteRecordAction);
	}

	public void enableForm(boolean enable) {
		saveButton.setEnabled(enable);
		deleteButton.setEnabled(enable);
		closeButton.setEnabled(enable);
		pricesButton.setEnabled(enable);
	}

	private JPanel createButtonBar() {
		JPanel panel = new JPanel();
		panel.add(saveButton);
		panel.add(pricesButton);
		panel.add(deleteButton);
		panel.add(closeButton);
		return panel;
	}

	private void updateLocale() {
		dialogHeaderTitle = config.get("clients.editor.header.text");
		dialogHeaderImage = new ImageIcon(config.get("clients.editor.header.icon"));
		nameLabel = config.get("clients.editor.name.text");
		phoneLabel = config.get("clients.editor.phone.text");
		emailLabel = config.get("clients.editor.email.text");
		addressLabel = config.get("clients.editor.address.text");
		homepageLabel = config.get("clients.editor.homepage.text");
		createDateLabel = config.get("clients.editor.createDate.text");
		lastUpdateLabel = config.get("clients.editor.updateDate.text");

		saveButton.setText(config.get("clients.editor.button.save.text"));
		saveButton.setIcon( new ImageIcon(config.get("clients.editor.button.save.icon")) );
		closeButton.setText(config.get("clients.editor.button.close.text"));
		closeButton.setIcon( new ImageIcon(config.get("clients.editor.button.close.icon")) );
		deleteButton.setText(config.get("clients.editor.button.delete.text"));
		deleteButton.setIcon( new ImageIcon(config.get("clients.editor.button.delete.icon")) );
		pricesButton.setText(config.get("clients.editor.button.prices.text"));
		pricesButton.setIcon( new ImageIcon(config.get("clients.editor.button.prices.icon")) );
	}

	public Clients getFormData() {
		Clients clients = new Clients();
		clients.setName( nameField.getText() );
		clients.setPhone( phoneField.getText() );
		clients.setEmail( emailField.getText() );
		clients.setHomePage( homepageField.getText() );
		clients.setAddress( addressField.getText() );
		return clients;
	}

	public void clearForm() {
		nameField.setText("");
		phoneField.setText("");
		emailField.setText("");
		homepageField.setText("");
		addressField.setText("");
	}

	public void setFormData(Clients client) {
		nameField.setText(client.getName());
		phoneField.setText(client.getPhone());
		emailField.setText(client.getEmail());
		homepageField.setText(client.getHomePage());
		addressField.setText(client.getAddress());
	}

}
